package com.wwwarehouse.financialcenter.fragment.moneywarehouse.billingrecords;

import android.os.Bundle;
import android.view.View;

import com.alibaba.fastjson.JSON;
import com.wwwarehouse.common.activity.BaseCardDeskActivity;
import com.wwwarehouse.common.adapter.pop.BubblePopAdapter;
import com.wwwarehouse.common.bean.filter.FilterBean;
import com.wwwarehouse.common.bean.filter.SeriesBean;
import com.wwwarehouse.common.bean.response.CommonClass;
import com.wwwarehouse.common.eventbus_event.DrawerSeriesCompleteEvent;
import com.wwwarehouse.common.fragment.DeskDrawerSeriesFragment;
import com.wwwarehouse.common.tools.PopUpUtils;
import com.wwwarehouse.financialcenter.R;
import com.wwwarehouse.financialcenter.bean.bill.ChargingRecordBean;
import com.wwwarehouse.financialcenter.bean.bill.SerializableMap;
import com.wwwarehouse.financialcenter.bean.bindingcard.MoneyHouseInfomationBean;
import com.wwwarehouse.financialcenter.constant.FinancialConstant;
import com.wwwarehouse.financialcenter.fragment.base.MyBaseViewPagerFragment;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.wwwarehouse.common.constant.Constant.sp_User_Id;

/**
 * @author Joseph on 2017/11/15.
 *         <p/>
 *         实时计费记录主页
 */

public class ChargingRecordPagerFragment extends MyBaseViewPagerFragment {

    private List<FilterBean> sortList; //排序
    private String mBusinessName = "";
    private String mBusinessId = "";
    private int whichSide = 3;//计费记录的哪方（供 1, 需 2）
    private  String   mSort = "";
    private  int   recordStatusCode = 7;//计费记录状态Code(1 未入账, 2 已入账, 4 已失效)
    private List<SeriesBean> mFiltList = new ArrayList<>(); //筛选
//    List<FilterBean> filterBuidsBeen = new ArrayList<>(); //业务单位
    List<FilterBean> filterChargingBean= new ArrayList<>(); //计费情况
    List<FilterBean> filterSideBean = new ArrayList<>(); //业务方
    private static final int GET_LIST_CHARGE_RECORD = 0;
    private Map mMap;
    private MoneyHouseInfomationBean.DataBean data;  //单个业务单位
//    private List<MoneyHouseInfomationBean.DataBean> moneyHomeDataList; //所有的业务单位
    private boolean isFirst = true;
    @Override
    public void setTitle() {
        if (peekFragment()instanceof ChargingRecordPagerFragment){
            mActivity.setTitle(getString(R.string.financial_center_charging_record));
        }
    }
    @Override
    protected boolean isRegisterEventBus() {
        return true;
    }
    @Override
    protected void loadDatas() {
        //排序
        sortList = new ArrayList<>();
        FilterBean filterBean = new FilterBean(false,"发生时间近到远");
        sortList.add( filterBean);

        //业务单位
        Bundle  bundle = getArguments();
        if (bundle !=null){
            data = (MoneyHouseInfomationBean.DataBean) bundle.getSerializable("moneyHomeData");
//            moneyHomeDataList = (List<MoneyHouseInfomationBean.DataBean>) bundle.getSerializable("moneyHomeDataList");
            if (data!=null){
                mBusinessId = data.getBusinessId()+"";
            }
//            if (moneyHomeDataList !=null&& moneyHomeDataList.size()>0){
//                for (int i = 0; i <moneyHomeDataList.size(); i++) {
//                    filterBuidsBeen.add(new FilterBean(false, moneyHomeDataList.get(i).getBusinessName(),moneyHomeDataList.get(i)));
//                }
//            }
        }
//        SeriesBean seriesBean = new SeriesBean();
//        seriesBean.setCount(1);
//        seriesBean.setSingle(true);
//        seriesBean.setTitle("所属业务单位");
//        seriesBean.setList(filterBuidsBeen);
//        mFiltList.add(seriesBean);

        //计费情况
        FilterBean mStatusBean = new FilterBean(false, "未入账");
        FilterBean mStatusBean1 = new FilterBean(false,"已入账");
        FilterBean mStatusBean2 = new FilterBean(false,"已失效");
        filterChargingBean.add(mStatusBean);
        filterChargingBean.add(mStatusBean1);
        filterChargingBean.add(mStatusBean2);
        SeriesBean seriesBean1 = new SeriesBean();
        seriesBean1.setCount(2);
        seriesBean1.setSingle(false);
        seriesBean1.setTitle("计费情况");
        seriesBean1.setList(filterChargingBean);
        mFiltList.add(seriesBean1);

        //业务方
        FilterBean mBean = new FilterBean(false,"需方");
        FilterBean mBean1 = new FilterBean(false,"供方");
        filterSideBean.add(mBean);
        filterSideBean.add(mBean1);
        SeriesBean seriesBean2 = new SeriesBean();
        seriesBean2.setCount(2);
        seriesBean2.setSingle(false);
        seriesBean2.setTitle("业务方");
        seriesBean2.setList(filterSideBean);
        mFiltList.add(seriesBean2);
        DeskDrawerSeriesFragment deskDrawerMultipleFragment = DeskDrawerSeriesFragment.newInstance(mFiltList);
        ((BaseCardDeskActivity) mActivity).setDrawerLayout(deskDrawerMultipleFragment);
    }

    @Override
    public void requestDatas() {
        mMap = new HashMap();

        mMap.put("businessId", mBusinessId);
        mMap.put("businessName", mBusinessName);
        mMap.put("whichSide", whichSide);
        mMap.put("recordStatusCode", recordStatusCode);
        mMap.put("page", 1);
        mMap.put("size", 1);
        mMap.put("userId", sp.getValue(sp_User_Id));
        httpPost(FinancialConstant.URL_LIST_CHARGE_RECORD, mMap, GET_LIST_CHARGE_RECORD, false, null);
    }
    public void onEventMainThread(DrawerSeriesCompleteEvent event) {
        if (peekFragment() instanceof ChargingRecordPagerFragment) {
            if (event.isReset()) {
                mBusinessName="";
                whichSide =3;
                recordStatusCode =7;
                ((BaseCardDeskActivity) mActivity).hideDrawerLayout();
            }else {
                List<SeriesBean> seriesList = event.getList();

//                List<FilterBean> businessFilter = seriesList.get(0).getList();
                List<FilterBean> paymentFilter = seriesList.get(0).getList();
                List<FilterBean> statusFilter = seriesList.get(1).getList();

//                for (FilterBean filterBean : businessFilter) {
//                    if (filterBean.isSelect()) {
//                        mBusinessName = filterBean.getText();
//                    }
//                }
                boolean   flag = false;
                recordStatusCode = 0 ;
                for (FilterBean filterBean : paymentFilter) {
                    if (filterBean.isSelect()) {
                        flag = true;
                        if ("未入账".equals(filterBean.getText())) {
                            recordStatusCode += 1;
                        } else if ("已入账".equals(filterBean.getText())){
                            recordStatusCode += 2;
                        }else {
                            recordStatusCode += 4;
                        }
                    }
                }
                if (!flag){
                    recordStatusCode = 7;
                }
                whichSide = 3;
                for (FilterBean filterBean : statusFilter) {
                    if (filterBean.isSelect()) {
                        if ("需方".equals(filterBean.getText())) {
                            whichSide -= 1;
                        }else {
                            whichSide -= 2;
                        }
                    }
                }
                if (whichSide==0){//都选了
                    whichSide = 3;
                }
            }
            requestDatas();
        }
    }
    @Override
    public void cancelClick() {
        super.cancelClick();
        mBusinessName = "";
        requestDatas();
    }
    @Override
    public void searchNoRealTimeClick(String text) {
        if (peekFragment() instanceof ChargingRecordPagerFragment){
            mBusinessName = text;
            requestDatas();
        }
    }
    @Override
    public void filterClick() {
        if (peekFragment() instanceof ChargingRecordPagerFragment) {
            ((BaseCardDeskActivity) mActivity).showDrawerLayout();
        }
    }
    @Override
    public void sortClick(View anchor) {
        if (peekFragment() instanceof ChargingRecordPagerFragment ){
            PopUpUtils.showBubbleDown(anchor, mActivity, sortList, new BubblePopAdapter.OnPopListClickListener() {
                @Override
                public void setOnItemClickListener(int i, View view) {
                    switch (i) {
                        case 0:
                            sortList.get(0).setSelect(true);
                            break;
                        default:
                            break;
                    }
                    //接口请求
                      requestDatas();
                }
            });
        }
    }

    @Override
    public void onDownPull() {
        if (isShow){
            ((BaseCardDeskActivity)mActivity).showSearchTitle();
            ((BaseCardDeskActivity)mActivity).hideSortBt();
        }else {
            ((BaseCardDeskActivity)mActivity).hideSearchTitle();
        }
        isShow = !isShow;
    }

    @Override
    public void onSuccess(CommonClass commonClass, int what) {
        switch (what){
            case GET_LIST_CHARGE_RECORD:
                if ("0".equals(commonClass.getCode())) {
                    ChargingRecordBean recordBean = JSON.parseObject(commonClass.getData().toString(), ChargingRecordBean.class);
                    int total = recordBean.getTotal();
                    if (total <= 0){
                        if (isFirst) {
                            isFirst = false;
                            mLoadingView.setVisibility(View.VISIBLE);
                            mLoadingView.showEmptyView(true);
                            mLoadingView.setEmptyListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    mLoadingView.showProgressView(true);
                                    isFirst = true;
                                    requestDatas();
                                }
                            });
                        } else {
                            ShowEmptyView();
                        }
                        return;
                    }
                    isFirst = false;
                    Bundle bundle = new Bundle();
                    SerializableMap myMap = new SerializableMap();
                    myMap.setMap(mMap);
                    bundle.putSerializable("map", myMap);
                    SetFragmentAndPage(total, 7, new ChargingRecordChildFragment().getClass().getName(), bundle);
                }
                break;
        }
    }

    @Override
    public void onReLoad(int code) {
        requestDatas();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        ((BaseCardDeskActivity)mActivity).showSortBt();
    }
}
